package com.zh.note.leetcode.greedy;

/**
 * 135. 分发糖果
 */
public class LC_135_candy {

    public int candy(int[] ratings) {
        int[] candy = new int[ratings.length];
        candy[0] = 1;
        //1.右边小孩比左边高的情况
        for (int i = 1; i < ratings.length; i++) {  // 从前向后
            if (ratings[i] > ratings[i - 1]) {
                candy[i] = candy[i - 1] + 1;
            } else {
                candy[i] = 1;
            }
        }
        //2.左边孩子比右边孩子高的情况
        for (int i = ratings.length - 2; i >= 0; i--) {  // 从后向前
            if (ratings[i] > ratings[i + 1]) {
                candy[i] = Math.max(candy[i + 1] + 1, candy[i]);
            }
        }
        int res = 0;
        for (int item : candy) {
            res += item;
        }
        return res;
    }
}
